
@inherits BootstrapComponentBase

<div @attributes="AdditionalAttributes" class="@ClassString">
    @if (ViewMode == CalendarViewMode.Month)
    {
        <div class="calendar-header" style="display: flex;color: black">
            <div style="font-size:1.2rem;font-weight: bold;margin-left: -15px ">
                答题日历
                <i class="bi bi-calendar-check" style="color: #666666"></i>
            </div>
            <div style="display: flex;justify-content: flex-end;font-size: 1rem;margin-right: -15px">
                <div style="margin: 0 3px;display: flex;align-items: center"><i class="bi bi-chevron-left" @onclick="() => OnChangeMonth(-1)"></i></div>
                <div style="margin: 0 3px;background-color:#e9ecef;border-radius: 5px;padding:2px 3px"><span>@NowMonth</span><span>月</span></div>
                <div style="margin: 0 3px;display: flex;align-items: center"><i class="bi bi-chevron-right" @onclick="() => OnChangeMonth(1)"></i></div>
            </div>
        </div>
        <div class="calendar-body">
            <table cellspacing="0" cellpadding="0" class="calendar-table" style="border: 0;">
                <thead>
                    <tr style="color: #666666">
                        @foreach (var w in WeekLists)
                        {
                            <th style="border: 0;">@w</th>
                        }
                    </tr>
                </thead>
                <tbody>
                    @for (var week = StartDate; week < EndDate; week = week.AddDays(7))
                    {
                        <tr >
                            @for (var index = 0; index < 7; index++)
                            {
                                var cellValue = CreateCellValue(week.AddDays(index));
                                if (CellTemplate == null)
                                {
                                    @RenderCell(cellValue)
                                }
                                else
                                {
                                    @RenderCellToday(cellValue)
                                }
                            }
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    }
    else
    {
        <div class="calendar-header">
            <div class="calendar-title"><span class="d-none d-sm-inline-block">@GetTitle()</span> <span>@WeekNumberText</span></div>
            <div class="calendar-button-group">
                <div class="btn-group">
                    <button type="button" class="btn btn-sm" @onclick="() => OnChangeWeek(-7)">
                        <span>@PreviousWeek</span>
                    </button>
                    <button type="button" class="btn btn-sm" @onclick="() => OnChangeWeek(0)">
                        <span>@WeekText</span>
                    </button>
                    <button type="button" class="btn btn-sm" @onclick="() => OnChangeWeek(7)">
                        <span>@NextWeek</span>
                    </button>
                </div>
            </div>
        </div>
        <div class="calendar-body">
            <table cellspacing="0" cellpadding="0" class="calendar-table table-week">
                <thead>
                    <tr>
                        @for (var index = 0; index < 7; index++)
                        {
                            @RenderWeekHeader(index)
                        }
                    </tr>
                </thead>
                <tbody>
                    @ChildContent
                </tbody>
            </table>
        </div>
    }
</div>

@code {
    private int[][] counts = new int[12][];

    protected override Task OnInitializedAsync()
    {
        for (int i = 0; i < counts.Length; i++)
        {
            counts[i] = new int[31]; // someSize 是您希望每个内层数组的大小
        }
        foreach (var a in AnsDetails)
        {
            if (a.UpdateTime != DateTime.MinValue)
            {
                counts[a.UpdateTime.Month-1][a.UpdateTime.Day-1]++;
            }
            else
            {
                counts[a.CreateTime.Month-1][a.CreateTime.Day-1]++;
            }
            
        }
        return base.OnInitializedAsync();
    }

    RenderFragment<CalendarCellValue> RenderCell => item =>
    @<td class="@GetCalendarCellString(item)" style="border: 0;">
        <div style="height: 40px !important;display: flex;align-items: center;justify-content: center">
            @if (item.CellValue.Month == NowMonth)
            {
                <span style="color: black">@item.CellValue.Day</span>
            }
            else
            {
                <span>@item.CellValue.Day</span>
            }
        </div>
        @if (counts[item.CellValue.Month-1][item.CellValue.Day-1] > 0)
        {
            <div style="padding-left:22px !important;margin-top: -40px !important;font-size:0.6rem">
                <Badge Color="Color.Success" IsPill="true"><span style="">@counts[item.CellValue.Month-1][item.CellValue.Day-1]</span></Badge>
            </div>
        }
    </td>;
    
    RenderFragment<CalendarCellValue> RenderCellToday => item =>
        @<td  style="border: 0;">
            <div style="height: 40px !important;display: flex;align-items: center;justify-content: center;border-radius: 50% !important;width: 40px !important;background-color: #28a745 !important;">
                <span style="color: white !important;">@item.CellValue.Day</span>
            </div>
        </td>;

    RenderFragment<int> RenderWeekHeader => index =>
    @<th>
        <div class="@GetWeekDayClassString(index)">
            <div><span class="d-none d-sm-inline-block">@WeekHeaderText</span>@WeekLists.ElementAtOrDefault(index)</div>
            <div>@GetWeekDayString(index)</div>
        </div>
    </th>;
}